<?php
include_once('../src/classes/class.mssql.php');
session_start();
$comtec_id_operation = $_GET["id_operation"];
$comtec_id_machine   = $_GET["id_machine"];
$comtec_id_mainteiner = $_GET["id_mainteiner"];
$token = $_GET["token"];

$db = new MSSQL();
$result = $db->fetch_array($db->query("SELECT cod_mainteiner,comtec_id_configman FROM machines WHERE comtec_id_machine = $comtec_id_machine"));   
$cod_mainteiner = $result['cod_mainteiner'];
$comtec_id_configman = $result['comtec_id_configman'];

$result = $db->fetch_array($db->query(
     "SELECT (r.name+' '+r.lastname1+' '+r.lastname2) as encargado,o.operation_type,
       
       DATEPART(day,m.date_next_maintenance) as next_day,                     
       DATEPART(month,m.date_next_maintenance) as next_month,
       DATEPART(yyyy,m.date_next_maintenance) as next_year, 
         
       DATEPART(day,o.operation_date_start) as op_day1,                     
       DATEPART(month,o.operation_date_start) as op_month1,
       DATEPART(yyyy,o.operation_date_start) as op_year1, 
       DATEPART(hour,o.operation_date_start) as hour1,
       DATEPART(minute,o.operation_date_start) as minute1, 
       
       DATEPART(day,o.operation_date_end) as op_day2,                     
       DATEPART(month,o.operation_date_end) as op_month2,
       DATEPART(yyyy,o.operation_date_end) as op_year2,  
       DATEPART(hour,o.operation_date_end) as hour2, 
       DATEPART(minute,o.operation_date_end) as minute2,  
        
       o.diagnosis,o.changed_parts,o.value_parts,o.value_work,o.value_others,o.value_total,o.notes,o.notes_for_owner,
       o.delay,r.comtec_id_responsible,o.comtec_id_status,o.comtec_id_order
      FROM (operation as o INNER JOIN machines as m ON o.comtec_id_machine=m.comtec_id_machine) INNER JOIN 
            responsible as r ON o.comtec_id_responsible = r.comtec_id_responsible WHERE o.comtec_id_operation = $comtec_id_operation"
)); 

 $operation_type        = $result['operation_type'];
 $comtec_id_responsible = $result['comtec_id_responsible'];
 $encargado             = $result['encargado'];
 $diagnosis             = $result['diagnosis'];
 $changed_parts         = $result['changed_parts'];
 $notes                 = $result['notes'];
 $comtec_id_order       = $result['comtec_id_order'];
 
 $next_day              = $result['next_day'];
 $next_month            = $result['next_month'];
 $next_year             = $result['next_year'];
 
 $op_day1               = $result['op_day1'];
 $op_month1             = $result['op_month1'];
 $op_year1              = $result['op_year1'];
 $hour1                 = $result['hour1'];
 $minute1               = $result['minute1'];
 
 $op_day2               = $result['op_day2'];
 $op_month2             = $result['op_month2'];
 $op_year2              = $result['op_year2'];
 $hour2                 = $result['hour2'];
 $minute2               = $result['minute2'];
 
 $value_parts = $result['value_parts'];
 $value_work = $result['value_work'];
 $value_others = $result['value_others'];
 $value_total = $result['value_total'];
 
 $delay      = $result['delay'];
 $comtec_id_status = $result['comtec_id_status'];
 $notes_for_owner = $result['notes_for_owner'];

function extra($x){
 if ($x <= 9){
  $x = "0".$x;   
 }   
 return $x;   
}

?>
<fieldset class="curved">
 <legend>Editar</legend>
  <table style="margin: 10px auto; border-spacing: 10px;">
      <tr>
          <td width="200">M&aacute;quina: </td>
          <td><input type="text" DISABLED size="40" value="<?=$cod_mainteiner;?>"/></td>
      </tr>
      <tr>
          <td width="200">Tipo de operaci&oacute;n</td>
          <td>
               <select id="operation_type2" onchange="javascript:operation_type()" style="width:257px;">
                       <?if($operation_type==0){?>
                       <option value="0" selected>Selecci&oacute;n...</option>
                       <?}else{?><option value="0">Selecci&oacute;n...</option><?}?>
                       <?if($operation_type==1){?>
                       <option value="1" selected>Mantenci&oacute;n</option>
                       <?}else{?><option value="1">Mantenci&oacute;n</option><?}?>
                       <?if($operation_type==2){?>
                       <option value="2" selected>Emergencia</option>
                       <?}else{?><option value="2">Emergencia</option><?}?>
                       <?if($operation_type==3){?>
                       <option value="3" selected>Instalaci&oacute;n</option>
                       <?}else{?><option value="3">Instalaci&oacute;n</option><?}?>
                       <?if($operation_type==4){?>
                       <option value="4" selected>Reparaci&oacute;n</option>
                       <?}else{?><option value="4">Reparaci&oacute;n</option><?}?>
               </select>
          </td>
          <td width="200">Encargado</td>
          <td>
              <select id="comtec_id_responsible2" style="width:257px;">
                    <option value="0" selected>Selecci&oacute;n...</option>
                    <?
                        $responsible = $db->query("SELECT comtec_id_responsible, name, lastname1, lastname2, status_reg FROM responsible WHERE comtec_id_mainteiner = $comtec_id_mainteiner ");
                        while ($r = sqlsrv_fetch_array($responsible,SQLSRV_FETCH_ASSOC))
                        {
                            if ($r['status_reg'] == 1)
                            {
                             if ($comtec_id_responsible==$r['comtec_id_responsible']){
                             echo "<option value=".$r['comtec_id_responsible']." selected>".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";
                             }else {
                              echo "<option value=".$r['comtec_id_responsible'].">".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";    
                             }
                             
                            }
                        }
                    ?>
               </select>
           </td>
      </tr>
        <tr>
               <td width="200">Diagn&oacute;stico</td>
               <td colspan=4><textarea id="diagnosis" rows="4" cols="91" style="resize:none"><?=$diagnosis?></textarea></td>
           </tr>
           <tr>
                <td width="200">Repuestos cambiados</td>
                <td colspan=4><textarea id="changed_parts" rows="4" cols="91" style="resize:none"><?=$changed_parts?></textarea></td>
           </tr>
           <tr>
                <td width="200">Notas</td>
                <td colspan=4><textarea id="notes" rows="3" cols="91" style="resize:none"><?=$notes?></textarea></td>
           </tr>
           <tr id="next_date" style="display: none">
               <td width="200">Fecha prox. Mantenci&oacute;n</td>
               <td>
                  <?
                  $optionsXML = simplexml_load_file("../xml/days.xml"); 
                  $options =  $optionsXML->xpath("//day/text()");
                  echo '<select id="next_day">';
                  while(list( $key , $value) = each($options)) {                       
                   if ($next_day == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/months.xml"); 
                  $options =  $optionsXML->xpath("//month/text()");
                  echo '<select id="next_month">';
                  while(list( $key , $value) = each($options)) {  
                   if ($next_month == $key) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/years.xml"); 
                  $options =  $optionsXML->xpath("//year/text()");
                  echo '<select id="next_year">';
                  while(list($key,$value) = each($options)) {   
                   if ($next_year == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
               </td>
           </tr>
           <? if($_SESSION['users']=="administratives-2" || $_SESSION['users']=="managers"){ ?>
           <tr>
                <td width="200">Valor mano de obra</td>
                <td><input type="text" id="value_work" size="40" value="<?=$value_work?>" onchange="javascript:addValue()" /></td>
                <td width="200">Valores repuestos</td>
                <td><input type="text" id="value_parts" size="40" value="<?=$value_parts?>" onchange="javascript:addValue()" /></td>
           </tr>
           <tr>
                <td width="200">Otros valores</td>
                <td><input type="text" id="value_others" size="40" value="<?=$value_others?>" onchange="javascript:addValue()" /></td>
                <td width="200">Valor total</td>
                <td><input type="text" size="40" id="value_total" DISABLED value="<?=$value_total?>" /></td>
           </tr>
           <? }?>
           <tr>              
               <td width="200">Fecha de inicio</td>
               <td>
                   <?
                     $optionsXML = simplexml_load_file("../xml/days.xml"); 
                     $options =  $optionsXML->xpath("//day/text()");
                     echo '<select id="op_day1" onchange="javascript:setDelay()">';
                     while(list( $key , $value) = each($options)) {      
                     if ($op_day1 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                     else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                     }
                     echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      echo '<select id="op_month1" onchange="javascript:setDelay()">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month1 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      echo '<select id="op_year1" onchange="javascript:setDelay()">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year1 == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                       else {echo '<option value='.$value.'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
               </td>
               <td width="200">Hora de inicio</td>
               <td>
                  <?
                  $optionsXML = simplexml_load_file("../xml/hours.xml"); 
                  $options =  $optionsXML->xpath("//hour/text()");
                  echo '<select id="hour1" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {   
                   if($value==$hour1){
                    echo '<option value='.$value.' selected>' . $value . '</option>';
                   }else{echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/minutes.xml"); 
                  $options =  $optionsXML->xpath("//minute/text()");
                  echo '<select id="minute1" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {  
                   if($minute1==$value){   
                    echo '<option value='.$value.' selected>' . $value . '</option>';
                   }else{echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?> 
               </td>
           </tr>   
           <tr>
               <td width="200">
                Fecha de fin
               </td>
               <td>
                   <?
                     $optionsXML = simplexml_load_file("../xml/days.xml"); 
                     $options =  $optionsXML->xpath("//day/text()");
                     echo '<select id="op_day2" onchange="javascript:setDelay()">';
                     while(list( $key , $value) = each($options)) {      
                     if ($op_day2 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                     else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                     }
                     echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      echo '<select id="op_month2" onchange="javascript:setDelay()">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month2 == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      echo '<select id="op_year2" onchange="javascript:setDelay()">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year2 == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                       else {echo '<option value='.$value.'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
               </td>
               <td width="200">
               Hora de fin
               </td>   
               <td>
                  <?
                  $optionsXML = simplexml_load_file("../xml/hours.xml"); 
                  $options =  $optionsXML->xpath("//hour/text()");
                  echo '<select id="hour2" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {  
                   if($hour2==$value){   
                    echo '<option value='.$value.' selected>' . $value . '</option>';
                   }else{echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/minutes.xml"); 
                  $options =  $optionsXML->xpath("//minute/text()");
                  echo '<select id="minute2" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) { 
                   if($value==$minute2){   
                    echo '<option value='.$value.' selected>' . $value . '</option>';
                   }else
                       {
                        echo '<option value='.$value.'>' . $value . '</option>';
                       }
                  }
                  echo '</select>';
                  ?> 
               </td> 
           </tr>
           <? if($_SESSION['users']=="administratives-2" || $_SESSION['users']=="managers"){ ?>
           <tr>
               <td width="200">Demora</td>
               <td><span id="delay"><?=isset($delay)? $delay:0?></span> minutos</td>
           </tr>
           <? }?>
           <? if($_SESSION['users']!="technicians" && $_SESSION['users']!="clients"){ ?>
           <tr>              
               <td width="200">Notas para cliente</td>
               <td colspan=4><textarea id="notes_for_owner" rows="2" cols="91" style="resize:none"><?=$notes_for_owner?></textarea></td>
           </tr>
           <? }?>
            <tr>
            <td width="200"><label>Estado</label></td> 
            <td><select id="status">
             <?
               $status = $db->query("SELECT id_s,name_s FROM status_op");
               while ($s = sqlsrv_fetch_array($status,SQLSRV_FETCH_ASSOC))
               {
                if ($comtec_id_status==$s['id_s']){
                 echo "<option value=".$s['id_s']." selected>".$s['name_s']."</option>";
                }else {
                 echo "<option value=".$s['id_s']." >".$s['name_s']."</option>";  
                }
               }
             ?>
             </select>
            </td>
           </tr>
           <tr>
               <td>
                   <input type="button" value="Editar" onclick="javascript:editOperationSearch(<?=$comtec_id_operation;?>);" />
                   <input type="button" value="Cancelar" onclick="javascript:goBackSearch();" />
               </td>
           </tr>
     </table>
  <input type="hidden" id="comtec_id_machine" value="<?=$comtec_id_machine;?>" />
  <input type="hidden" id="comtec_id_order" value="<?=$comtec_id_order;?>" />
  <input type="hidden" id="token" value="<?=$token;?>" />
  <div id="resultSearch"></div> 
</fieldset>
